package com.lhy.admin.web;

import com.lhy.admin.common.secure.authorization.Permissions;
import com.lhy.admin.model.Dict;
import com.lhy.admin.model.extend.DictAttribute;
import com.lhy.admin.service.DictService;
import com.lhy.commonweb.model.Page;
import com.lhy.commonweb.model.RequestPage;
import com.lhy.commonweb.model.ResponseResult;
import com.lhy.commonweb.service.AbstractService;
import com.lhy.commonweb.web.BaseControllerImpl;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("dict")
@Permissions
public class DictController extends BaseControllerImpl<Dict, Integer> {

    public DictController(@Qualifier("dictService") AbstractService service) {
        super(service);
    }

    @Override
    @Permissions("dict.view")
    public Page<Dict> query(RequestPage requestPage, Dict bean) {
        return service.query(requestPage.getPage(), requestPage.getRows(), requestPage.getSc(), bean);
    }

    @Permissions("dict.view")
    @RequestMapping(value = "list/{code}", method = RequestMethod.GET)
    public List<Dict> list(@PathVariable("code") String code) {
        return DictService.class.cast(service).listByCode(code);
    }

    @Permissions(value = {"dict.add", "dict.edit"})
    @RequestMapping(value = "updateWithAttribute", method = RequestMethod.POST)
    public ResponseResult updateWithAttribute(@RequestBody DictAttribute bean) {
        DictService.class.cast(service).updateDict(bean);
        return ResponseResult.ok();
    }

    @Override
    @Permissions("dict.del")
    public ResponseResult delete(@PathVariable("id") Integer id) {
        return super.delete(id);
    }

    @Override
    @Permissions("dict.del")
    public ResponseResult deleteCond(@Validated @ModelAttribute Dict bean) {
        return super.deleteCond(bean);
    }
}
